package com.xxwy.dependency.injection;

import com.xxwy.dependency.User;
import com.xxwy.dependency.UserRepository;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author wy
 * @date 2020/4/20 11:04
 */
public class Demo {

  public static void main(String[] args) {
    BeanFactory beanFactory = new ClassPathXmlApplicationContext("classpath:/META-INF/dependency-injection-context.xml");

    UserRepository bean = beanFactory.getBean(UserRepository.class);
    System.out.println(bean);

    System.out.println(bean.getBeanFactory() == beanFactory);

    //依赖注入：输出注入了BeanFactory.class 找得到
    System.out.println(bean.getBeanFactory());

    //依赖查找：输出注入的BeanFactory.class对象，找不到BeanFactory.class对象：NoSuchBeanDefinitionException
    //System.out.println(beanFactory.getBean(BeanFactory.class));


    //延迟依赖注入，自定义Bean
    ObjectFactory<User> userObjectFactory = bean.getUserObjectFactory();
    User user = userObjectFactory.getObject();
    System.out.println(user);

    //延迟依赖注入，容器非Bean,用户生成的对象
    ObjectFactory<ApplicationContext> objectFactory = bean.getApplicationObjectFactory();
    ApplicationContext application = objectFactory.getObject();
    System.out.println(application == beanFactory); //true

  }
}
